﻿using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace SHomeWorkshop.LunarMind.Dialogs
{
    /// <summary>
    /// Interaction logic for InputWindow.xaml
    /// </summary>
    public partial class InputWindow : Window
    {
        public InputWindow(Window parentWindow, string promptText, string defaultText,
            string windowTitle, char[] invalidatedChars = null, bool closeInputMethod = false)
        {
            InitializeComponent();
            this.Owner = parentWindow;

            PromptText = promptText;
            InputedText = defaultText;

            this.Title = windowTitle;
            this.invalidatedChars = invalidatedChars;

            inputTextBox.Focus();
            inputTextBox.SelectAll();

            if (closeInputMethod)
            {
                Globals.SwitchInputMethod(false);
            }
            else
            {
                Globals.SwitchInputMethod(true);
            }
        }

        public string PromptText
        {
            get { return promptTextBlock.Text; }
            set { promptTextBlock.Text = value; }
        }

        private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                buttonOk_Click(sender, e);
            }
            else if (e.Key == Key.Escape)
            {
                this.DialogResult = false;
                this.Close();
            }
            else if (e.Key == Key.F1)
            {
                if (this.invalidatedChars != null &&
                    this.invalidatedChars.Length > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (char c in invalidatedChars)
                    {
                        sb.Append(c); sb.Append(' ');
                    }

                    MessageBox.Show("禁用字符包括：\r\n\r\n" + sb.ToString(),
                        this.Title, MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }

        private char[] invalidatedChars = null;

        public char[] InvalidatedChars
        {
            get { return invalidatedChars; }
            set { invalidatedChars = value; }
        }

        private bool ValidateInputedText()
        {
            if (invalidatedChars == null || invalidatedChars.Length <= 0) return true;

            bool haveInvalidatedChar = false;
            foreach (char c in invalidatedChars)
            {
                if (inputTextBox.Text.Contains(c))
                {
                    haveInvalidatedChar = true;
                    break;
                }
            }

            if (haveInvalidatedChar) return false;

            return true;
        }

        public string InputedText
        {
            get { return inputTextBox.Text; }
            set { inputTextBox.Text = value; }
        }

        private void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateInputedText())
            {
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                this.tbkToolTip.Text = "输入了禁用字符！";
                this.tbkToolTip.ToolTip = "按F1显示禁用字符";
            }
        }

        private void inputTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (inputTextBox.Text == "")
            {
                VisualBrush vbrush = (VisualBrush)this.FindResource("textBoxBackground");
                inputTextBox.Background = vbrush;

            }
            else
            {
                inputTextBox.Background = Brushes.White;
            }
        }
    }
}
